import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Test23 {
    /**
     * 目的:获取线程的返回值
     * 方法:使用Callable类和FutureTask类
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable = new Callable<Integer>() {
            int count = 0;
            @Override
            public Integer call() throws Exception {
                for (int i = 0; i <= 1000; i++) {
                    count += i;
                }
                return count;
            }
        };
        FutureTask<Integer> futureTask = new FutureTask<Integer>(callable);
        Thread thread = new Thread(futureTask);
        thread.start();
        int count = futureTask.get();
        System.out.println(count);
    }
}
